package com.fzy.core.tags;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.fzy.core.utils.StringUtils;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;

public class ExtFormFieldTag extends BodyTagSupport {

	private static final long serialVersionUID = -6077400408031503961L;

	String labelname;
	String labelwidth;
	String fieldname;
	String fieldwidth;
	
	private String bodyHtml;

	public int doStartTag() throws JspException {
		return super.doStartTag();
	}

	public void doInitBody() throws JspException {
		super.doInitBody();
	}

	public int doAfterBody() throws JspException {
		BodyContent bodycontent = getBodyContent();
		bodyHtml = bodycontent.getString();
		return super.doAfterBody();
	}

	public int doEndTag() throws JspException {
		List matchedErrors = new LinkedList();
		boolean hasError = false;
		
		if(!StringUtils.isBlank(fieldname)){
            if (fieldname.startsWith("%{") && fieldname.endsWith("}")) {
            	fieldname = fieldname.substring(2, fieldname.length() - 1);
            	fieldname = (String)ActionContext.getContext().getValueStack().findValue(fieldname);
			}
			Object action = ActionContext.getContext().getActionInvocation().getAction();
			if(action != null && action instanceof ActionSupport){
				Map fieldErrors = ((ActionSupport)action).getFieldErrors();
				if(fieldErrors != null && fieldErrors.size() > 0){
					for (Iterator iterator = fieldErrors.keySet().iterator(); iterator
							.hasNext();) {
						String key = (String) iterator.next();
						if(fieldname.equals(key)){
							hasError = true;
							Object error = fieldErrors.get(key);
							if(error != null){
								if(error instanceof List){
									List x = (List)error;
									matchedErrors.addAll(x);
								}else{
									matchedErrors.add(error.toString());
								}
							}
						}
					}
				}
			}
			
		}
		
		
		String html = "";
		
		int intLabelWidth = TagUtils.toIntWidth(labelwidth, 130);
		int intFieldWidth = TagUtils.toIntWidth(fieldwidth, 150);
		html += "<table cellpadding=\"0\"";
		html += "	style=\"border-width: 0pt; table-layout: fixed;\"";
		html += "	class=\"x-field x-form-item x-field-default x-anchor-form-item x-form-dirty"+(hasError ? " x-form-invalid" : "")+"\">";
		html += "	<tbody>";
		html += "		<tr>";
		if(!StringUtils.isBlank(labelname)){
            if (labelname.startsWith("%{") && labelname.endsWith("}")) {
            	labelname = labelname.substring(2, labelname.length() - 1);
            	labelname = (String)ActionContext.getContext().getValueStack().findValue(labelname);
			}
			html += "			<td valign=\"top\" class=\"x-field-label-cell\"";
			html += "				halign=\"left\" style=\"width: "+intLabelWidth+"px;\"><label";
			html += "				style=\"width: 100%; margin-right: 5px;\"";
			html += "				class=\"x-form-item-label x-form-item-label-left\">";
			html += labelname;
			html += "					</label></td>";
		}
		html += "			<td colspan=\"1\" class=\"x-form-item-body \"";
		html += "				style=\"width: "+intFieldWidth+"px;\">";
		
		
		html += bodyHtml;
		
		
		html += "			</td>";
		html += "			<td width=\"18\" valign=\"middle\" style=\"\">";
		html += "				<div";
		html += "					style=\"width: 18px; display: "+(hasError ? "block" : "none")+";\"";
		html += "					class=\"x-form-error-msg x-form-invalid-icon\">";
		if(hasError){
			html += "					<ul>";
			for (Iterator iterator = matchedErrors.iterator(); iterator
					.hasNext();) {
				String error = (String)iterator.next();
				html += "						<li>"+error+"</li>";
			}
			html += "					</ul>";
		}
		html += "				</div></td>";
		html += "		</tr>";
		html += "	</tbody>";
		html += "</table>";
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	public String getLabelname() {
		return labelname;
	}

	public void setLabelname(String labelname) {
		this.labelname = labelname;
	}

	public String getLabelwidth() {
		return labelwidth;
	}

	public void setLabelwidth(String labelwidth) {
		this.labelwidth = labelwidth;
	}

	public String getFieldname() {
		return fieldname;
	}

	public void setFieldname(String fieldname) {
		this.fieldname = fieldname;
	}

	public String getFieldwidth() {
		return fieldwidth;
	}

	public void setFieldwidth(String fieldwidth) {
		this.fieldwidth = fieldwidth;
	}

}
